/*-------------------------------------------------------------------------
 *
 *	  GB18030 <--> UTF8
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
#include "../../Unicode/gb18030_to_utf8.map"
#include "../../Unicode/utf8_to_gb18030.map"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(gb18030_to_utf8);
PG_FUNCTION_INFO_V1(utf8_to_gb18030);

/*
 * 将 4 字节 GB18030 字符转换为线性代码空间
 *
 * 第一和第三字节的范围为 0x81 到 0xfe（126 个值），
 * 而第二和第四字节的范围为 0x30 到 0x39（10 个值）。
 */
static inline uint32 fc_gb_linear(uint32 fc_gb)
{
	uint32		fc_b0 = (fc_gb & 0xff000000) >> 24;
	uint32		fc_b1 = (fc_gb & 0x00ff0000) >> 16;
	uint32		fc_b2 = (fc_gb & 0x0000ff00) >> 8;
	uint32		fc_b3 = (fc_gb & 0x000000ff);

	return fc_b0 * 12600 + fc_b1 * 1260 + fc_b2 * 10 + fc_b3 -
		(0x81 * 12600 + 0x30 * 1260 + 0x81 * 10 + 0x30);
}

static inline uint32 fc_gb_unlinear(uint32 fc_lin)
{
	uint32		fc_r0 = 0x81 + fc_lin / 12600;
	uint32		fc_r1 = 0x30 + (fc_lin / 1260) % 10;
	uint32		fc_r2 = 0x81 + (fc_lin / 10) % 126;
	uint32		fc_r3 = 0x30 + fc_lin % 10;

	return (fc_r0 << 24) | (fc_r1 << 16) | (fc_r2 << 8) | fc_r3;
}

/*
 * 将字格式的 UTF8 转换为 Unicode 代码点
 *
 * 可能应该放到其他地方...
 */
static inline uint32 fc_unicode_to_utf8word(uint32 fc_c)
{
	uint32		fc_word;

	if (fc_c <= 0x7F)
	{
		fc_word = fc_c;
	}
	else if (fc_c <= 0x7FF)
	{
		fc_word = (0xC0 | ((fc_c >> 6) & 0x1F)) << 8;
		fc_word |= 0x80 | (fc_c & 0x3F);
	}
	else if (fc_c <= 0xFFFF)
	{
		fc_word = (0xE0 | ((fc_c >> 12) & 0x0F)) << 16;
		fc_word |= (0x80 | ((fc_c >> 6) & 0x3F)) << 8;
		fc_word |= 0x80 | (fc_c & 0x3F);
	}
	else
	{
		fc_word = (0xF0 | ((fc_c >> 18) & 0x07)) << 24;
		fc_word |= (0x80 | ((fc_c >> 12) & 0x3F)) << 16;
		fc_word |= (0x80 | ((fc_c >> 6) & 0x3F)) << 8;
		fc_word |= 0x80 | (fc_c & 0x3F);
	}

	return fc_word;
}

static inline uint32 fc_utf8word_to_unicode(uint32 fc_c)
{
	uint32		fc_ucs;

	if (fc_c <= 0x7F)
	{
		fc_ucs = fc_c;
	}
	else if (fc_c <= 0xFFFF)
	{
		fc_ucs = ((fc_c >> 8) & 0x1F) << 6;
		fc_ucs |= fc_c & 0x3F;
	}
	else if (fc_c <= 0xFFFFFF)
	{
		fc_ucs = ((fc_c >> 16) & 0x0F) << 12;
		fc_ucs |= ((fc_c >> 8) & 0x3F) << 6;
		fc_ucs |= fc_c & 0x3F;
	}
	else
	{
		fc_ucs = ((fc_c >> 24) & 0x07) << 18;
		fc_ucs |= ((fc_c >> 16) & 0x3F) << 12;
		fc_ucs |= ((fc_c >> 8) & 0x3F) << 6;
		fc_ucs |= fc_c & 0x3F;
	}

	return fc_ucs;
}

/*
 * 执行 GB18030 范围到 UTF8 的映射
 *
 * 我们需要转换的范围在 gb-18030-2000.xml 中指定。
 * 所有都是 4 字节 GB18030 代码的范围。
 */
static uint32 fc_conv_18030_to_utf8(uint32 fc_code)
{
#define conv18030(minunicode, mincode, maxcode) \
	if (fc_code >= mincode && fc_code <= maxcode) \
		return fc_unicode_to_utf8word(fc_gb_linear(fc_code) - fc_gb_linear(mincode) + minunicode)

	conv18030(0x0452, 0x8130D330, 0x8136A531);
	conv18030(0x2643, 0x8137A839, 0x8138FD38);
	conv18030(0x361B, 0x8230A633, 0x8230F237);
	conv18030(0x3CE1, 0x8231D438, 0x8232AF32);
	conv18030(0x4160, 0x8232C937, 0x8232F837);
	conv18030(0x44D7, 0x8233A339, 0x8233C931);
	conv18030(0x478E, 0x8233E838, 0x82349638);
	conv18030(0x49B8, 0x8234A131, 0x8234E733);
	conv18030(0x9FA6, 0x82358F33, 0x8336C738);
	conv18030(0xE865, 0x8336D030, 0x84308534);
	conv18030(0xFA2A, 0x84309C38, 0x84318537);
	conv18030(0xFFE6, 0x8431A234, 0x8431A439);
	conv18030(0x10000, 0x90308130, 0xE3329A35);
	/* 不存在映射 */
	return 0;
}

/*
 * 执行 UTF8 范围到 GB18030 的映射
 */
static uint32 fc_conv_utf8_to_18030(uint32 fc_code)
{
	uint32		fc_ucs = fc_utf8word_to_unicode(fc_code);

#define convutf8(minunicode, maxunicode, mincode) \
	if (fc_ucs >= minunicode && fc_ucs <= maxunicode) \
		return fc_gb_unlinear(fc_ucs - minunicode + fc_gb_linear(mincode))

	convutf8(0x0452, 0x200F, 0x8130D330);
	convutf8(0x2643, 0x2E80, 0x8137A839);
	convutf8(0x361B, 0x3917, 0x8230A633);
	convutf8(0x3CE1, 0x4055, 0x8231D438);
	convutf8(0x4160, 0x4336, 0x8232C937);
	convutf8(0x44D7, 0x464B, 0x8233A339);
	convutf8(0x478E, 0x4946, 0x8233E838);
	convutf8(0x49B8, 0x4C76, 0x8234A131);
	convutf8(0x9FA6, 0xD7FF, 0x82358F33);
	convutf8(0xE865, 0xF92B, 0x8336D030);
	convutf8(0xFA2A, 0xFE2F, 0x84309C38);
	convutf8(0xFFE6, 0xFFFF, 0x8431A234);
	convutf8(0x10000, 0x10FFFF, 0x90308130);
	/* 不存在映射 */
	return 0;
}

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */
Datum gb18030_to_utf8(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_GB18030, PG_UTF8);

	fc_converted = LocalToUtf(fc_src, fc_len, fc_dest,
						   &gb18030_to_unicode_tree,
						   NULL, 0,
						   fc_conv_18030_to_utf8,
						   PG_GB18030,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum utf8_to_gb18030(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_GB18030);

	fc_converted = UtfToLocal(fc_src, fc_len, fc_dest,
						   &gb18030_from_unicode_tree,
						   NULL, 0,
						   fc_conv_utf8_to_18030,
						   PG_GB18030,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}
